﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class AddNewAccount : System.Web.UI.UserControl
    {
        public int qlaccount;
        public int qltour;
        public int qlmenu;
        public int qlhoadon;
        public int qlthongtin;
        public int qlgiaodien;
        public int nextidaccount;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlRole.Text.Equals("Mod"))
            {
                lblqlmenu.Visible = true;
                cbxqlaccount.Visible = true;
                cbxqlgiaodien.Visible = true;
                cbxqlhoadon.Visible = true;
                cbxqlmenu.Visible = true;
                cbxqlthongtin.Visible = true;
                cbxqltour.Visible = true;
            }
            else
            {
                lblqlmenu.Visible = false;
                cbxqlaccount.Visible = false;
                cbxqlgiaodien.Visible = false;
                cbxqlhoadon.Visible = false;
                cbxqlmenu.Visible = false;
                cbxqlthongtin.Visible = false;
                cbxqltour.Visible = false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Account", "Account_ID"));
            string username = txtUserAccount.Text;
            string password = txtPassword.Text;
            string fullname = txtFullname.Text;
            string email = txtEmail.Text;
            string phonenumber = txtPhone.Text;
            string address = txtAddress.Text;
            string role = ddlRole.Text;
            string filePath = null;
            if (cbxqlaccount.Checked == true)
            {
                qlaccount = 1;
            }
            else
            {
                qlaccount = 0;
            }
            if (cbxqlgiaodien.Checked == true)
            {
                qlgiaodien = 1;
            }
            else
            {
                qlgiaodien = 0;
            }
            if (cbxqlhoadon.Checked == true)
            {
                qlhoadon = 1;
            }
            else
            {
                qlhoadon = 0;
            }
            if (cbxqltour.Checked == true)
            {
                qltour = 1;
            }
            else
            {
                qltour = 0;
            }
            if (cbxqlmenu.Checked == true)
            {
                qlmenu = 1;
            }
            else
            {
                qlmenu = 0;
            }
            if (cbxqlthongtin.Checked == true)
            {
                qlthongtin = 1;
            }
            else
            {
                qlthongtin = 0;
            }


            string password_mahoa = LIB.Converter.Encode("sha1", password);
            DataTable checkuseremail = DAL.DCollector.DBCollector.Instance.StoredProcedureForgetPassword("checkuseraccountexit", username, email);
            if (checkuseremail.Rows[0]["result"].ToString() == "0")
            {
                if (fulImageAccount.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImageAccount.FileName))
                    {
                        if (role.Equals("Admin"))
                        {
                            nextidaccount = nextid + 1;
                            string iamgeaccount = fulImageAccount.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Account/"), iamgeaccount, 250, fulImageAccount.PostedFile.InputStream);
                            filePath = "/Resources/Images/Images-Account/" + fulImageAccount.FileName;
                            string value = "" + nextidaccount + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "',1,1,1,1,1,1";
                            List<string> values = new List<string>();
                            values.Add(value);
                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertonerow", values, false);
                            txtUserAccount.Text = "";
                            txtPassword.Text = "";
                            txtPhone.Text = "";
                            txtFullname.Text = "";
                            txtEmail.Text = "";
                            txtAddress.Text = "";
                            txtUserAccount.Text = "";
                            Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                        }
                        else
                        {
                            nextidaccount = nextid + 1;
                            string iamgeaccount = fulImageAccount.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Account/"), iamgeaccount, 250, fulImageAccount.PostedFile.InputStream);
                            filePath = "/Resources/Images/Images-Account/" + fulImageAccount.FileName;
                            string value = "" + nextidaccount + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "'," + qlaccount + "," + qltour + "," + qlmenu + "," + qlhoadon + "," + qlthongtin + "," + qlgiaodien + "";
                            List<string> values = new List<string>();
                            values.Add(value);
                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertonerow", values, false);
                            txtUserAccount.Text = "";
                            txtPassword.Text = "";
                            txtPhone.Text = "";
                            txtFullname.Text = "";
                            txtEmail.Text = "";
                            txtAddress.Text = "";
                            txtUserAccount.Text = "";
                            Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                        }
                    }
                    else
                    {
                        lblCheckFileImages.Text = "Chỉ có thể upload file(.jpg,.png,.jpeg)";
                    }
                }
                else
                {
                    if (nextid == 0)
                    {
                        string value = "1,N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "'," + qlaccount + "," + qltour + "," + qlmenu + "," + qlhoadon + "," + qlthongtin + "," + qlgiaodien + "";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertonerow", values, false);
                        //Session["UsernameAccount"] = txtUserAccount.Text;
                        Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                    }
                    else
                    {
                        nextidaccount = nextid + 1;
                        string value = "" + nextidaccount + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "'," + qlaccount + "," + qltour + "," + qlmenu + "," + qlhoadon + "," + qlthongtin + "," + qlgiaodien + "";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertonerow", values, false);
                        //Session["UsernameAccount"] = txtUserAccount.Text;
                        Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                    }

                }
            }
            else
            {
                Response.Write("<script>alert('Tên tài khoản hoặc Email đã tồn tại')</script>");
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtUserAccount.Text = "";
            txtPassword.Text = "";
            txtPhone.Text = "";
            txtFullname.Text = "";
            txtEmail.Text = "";
            txtAddress.Text = "";
            txtUserAccount.Text = "";
        }
    }
}